﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ProgressBars
{
    public partial class ProgBarsDisp : UserControl
    {
        delegate bool CreateProgBar(object prgBar);
        delegate void DestroyProgBar(string prgBar);

        public ProgBarsDisp()
        {
            InitializeComponent();
        }

        const int V_OFFSET = 5;
        const int INDENT = 10;
        const int S_LIMIT = 25;

        bool addProgressBar2Panel(object obj)
        {
            ProgressBar prgBar = (ProgressBar)obj;

            string setName = prgBar.Name;
            int incrName = 1;
            for (int i = 0; i < pnlHolder.Controls.Count; i++)
            {
                if (pnlHolder.Controls[i].Name == prgBar.Name)
                    //return false;
                    prgBar.Name = setName + incrName++.ToString();
            }
            
            Label progLabel = new Label();
            progLabel.Text = progLabel.Name = prgBar.Name;

            if (pnlHolder.Controls.Count > 0)
                progLabel.Location = new Point(pnlHolder.Controls[pnlHolder.Controls.Count - 1].Location.X, pnlHolder.Controls[pnlHolder.Controls.Count - 1].Location.Y + pnlHolder.Controls[pnlHolder.Controls.Count - 1].Size.Height + V_OFFSET);
            else
                progLabel.Location = new Point(INDENT, V_OFFSET);

            pnlHolder.Controls.Add(progLabel);
            progLabel.Size = new Size(Width - progLabel.Location.X - S_LIMIT, progLabel.Size.Height / 2);

            prgBar.Location = new Point(pnlHolder.Controls[pnlHolder.Controls.Count - 1].Location.X, pnlHolder.Controls[pnlHolder.Controls.Count - 1].Location.Y + pnlHolder.Controls[pnlHolder.Controls.Count - 1].Size.Height + V_OFFSET);

            pnlHolder.Controls.Add(prgBar);
            prgBar.Size = new Size(Width - prgBar.Location.X - S_LIMIT, prgBar.Size.Height);

            pnlHolder.ScrollControlIntoView(prgBar);

            return true;
        }

        void removeProgressBarFromPanel(string prgBarName)
        {
            if (InvokeRequired)
            {
                Invoke(new DestroyProgBar(removeProgressBarFromPanel), new object[] { prgBarName });
            }
            else
            {
                int toRemCounter = 0;
                Control[] toRemove = new Control[2];
                for (int i = 0; i < pnlHolder.Controls.Count; i++)
                {
                    if (pnlHolder.Controls[i].Name == prgBarName)
                        toRemove[toRemCounter++] = pnlHolder.Controls[i];
                }

                pnlHolder.Controls.Remove(toRemove[0]);
                pnlHolder.Controls.Remove(toRemove[1]);
                toRemove[0].Dispose();
                toRemove[1].Dispose();
            }
        }

        public ProgBarsHdl CreateProgressBar(string name)
        {
            ProgressBar newProgBar = new ProgressBar();
            newProgBar.Name = name;
            newProgBar.Size = new Size(newProgBar.Size.Width, newProgBar.Size.Height / 2);

            if ((bool)Invoke(new CreateProgBar(addProgressBar2Panel), new object[] { newProgBar }))
                return new ProgBarsHdl(name, newProgBar);
            else
                return null;
        }

        public void DestroyProgressBar(ProgBarsHdl progHdl)
        {
            removeProgressBarFromPanel(progHdl.ProgBarText);
        }

        public int GetActiveProgressBars()
        {
            return pnlHolder.Controls.Count;
        }
    }
}
